<?php
include_once("model/partner.class.php");
include_once("includes/util.php");
include_once("controller/JKUpload.class.php");

class partnerman 
{
	var $db;
 	var $smarty;
 	
    function partnerman($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	function addPartner()
	{
		$partner_status = 0;
		if(isset($_POST['partner_status']))
			$partner_status = 1;
		$msg = partner::validatePartner();	
		if($msg == ""){
			if($_FILES['partner_image']['name'] != NULL && $_FILES['partner_image']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['partner_image']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				$namefile = explode(".".$image_type,$_FILES['partner_image']['name']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_product_thumbnail($_FILES['partner_image']['tmp_name'], $namefile['0'], $image_type);
				} 
			}
			$partner = array (
					'partner_name' => $_POST['partner_name'],
					'partner_image'=>$imagename,
					'partner_status'=>$partner_status,
					'partner_url'=>$_POST['partner_url']
					);
			$id = partner::savePartner($this->db, $partner);
			if($id >=0 ){
				$msg = "Partner has been added.";
				//Merge POST information to return back client and show at client side
				$arr = array_merge($arr, $_POST);
			}
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=partnerman") ;
		}else{
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=partnerman&sub1=addpartner") ;	
		}
		return;
	}

	
	function editPartner(){
		if(isset( $_POST['partner_id'])){
			$msgId = 2;
			$msg = partner::validatePartner();	
			if($msg == "")
			{
				if(isset($_POST['partner_status']))
					$partner_status = 1;
				$partner =  partner::getPartnerById($this->db,$_POST['partner_id']);
				if($_FILES['partner_image']['name'] != NULL && $_FILES['partner_image']['name'] != "") {
					$upload = new JKUpload();
					$image = $_FILES['partner_image']['name'];
					$path = pathinfo($image);
					$image_type = strtolower($path['extension']);
					$namefile = explode(".".$image_type,$_FILES['partner_image']['name']);
					if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
						$imagename = $upload->upload_product_thumbnail($_FILES['partner_image']['tmp_name'], $namefile['0'], $image_type);
					} 
					@unlink("../".UPLOAD_PRODUCT.$partner->partner_image);
					@unlink("../".UPLOAD_PRODUCT_THUMB.$partner->partner_image);
				}else{
					$imagename = $partner->partner_image;
				}
				$partner = array (
					'partner_id' => $_POST['partner_id'],
					'partner_name' => $_POST['partner_name'],
					'partner_image'=>$imagename,
					'partner_status'=>$partner_status,
					'partner_url'=>$_POST['partner_url']
					);
				$res = partner::updatePartner($this->db, $partner);

				$msgId = 1;
				setcookie("message", $msg,time()+5);
				header("Location: ./?mod=partnerman") ;
			}else{
				setcookie("message", $msg,time()+5);
				header("Location: ./?mod=partnerman&sub1=editpartner&id=".$_POST['partner_id']) ;	
			}
		}	
	}
	
	function deletePartner(){
		$arr = array("code" => -1, "message" => _("Could not delete partner."));
				
				$res = partner::deletePartner($this->db, $_POST['id']);
				
				if($res)
				{
					$arr = array("code" => 0, 
					"message" => sprintf(_("Partner with id %s has been deleted."), 
											'<b>' . $_POST['id'] . '</b>'));
					@unlink("../".UPLOAD_PRODUCT.$_POST['partner_image']);
					@unlink("../".UPLOAD_PRODUCT_THUMB.$_POST['partner_image']);
				}
				
				echo array2json($arr);
				return;
	}
 	function process()
	{
		if(isset($_POST['act']))
		{
			$act = $_POST['act'];
			
			if($act == "addpartner")
				$this->addPartner();
			else if($act == "delpartner")
				$this->deletePartner();
			else if($act == "editpartner")
				$this->editPartner();
		}
		else{
			if(isset($_GET['sub1']) && $_GET['sub1'] == 'editpartner'){
				$partner = partner::getPartnerById($this->db, $_GET['id']);
				$this->smarty->assign('PARTNER', $partner);
			}else{	
				$allpartner = partner::getAllPartner($this->db);
				$this->smarty->assign("ALLPARTNER", $allpartner);
			}
			$this->smarty->assign("PATH","../".UPLOAD_PRODUCT_THUMB);
			$this->smarty->display('partner.tpl');
		}
	}
}
?>